<?php
/***********************************************************************
 * @package    	IvanEngine
 * @subpackage 	Core
 * @author     	Ivan <ivanzx@msn.com>
 * @created    	2010-05-17
 ***********************************************************************/
if(defined("IVANENGINE_CORE_VALIDATOR_STRING"))
	die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_CORE_VALIDATOR_STRING", true);

require_once (dirname(__FILE__).'/ValidatorBase.php');

class CValidatorString extends CValidatorBase 
{
	const NM_MaxLength	= 'NM_MaxLength';
	const NM_MinLength	= 'NM_MinLength';
	const NO_MaxLength	= 'NO_MaxLength';
	const NO_MinLength	= 'NO_MinLength';
	protected function Initialize($options = array(), $messages = array())
  	{
  		parent::Initialize($options, $messages);
  		
  		$this->AddMessage(self::NM_MaxLength, '"%value%" is too long (%max_length% characters max).');
	    $this->AddMessage(self::NM_MinLength, '"%value%" is too short (%min_length% characters min).');
	
	    $this->AddOption(self::NO_MaxLength, 256);
	    $this->AddOption(self::NO_MinLength, 0);
	
	    $this->SetOption(self::NO_EmptyValue, '');
  	}
  	
	protected function DoVerify($value) 
	{
		$valid = (string)$value;

	    $length = function_exists('mb_strlen') ? mb_strlen($valid, $this->GetCharset()) : strlen($valid);
	
	    if ($this->HasOption(self::NO_MaxLength) && $length > $this->GetOption('max_length'))
	    {
	      	throw new CExceptionValidator($this, self::NM_MaxLength, array('value' => $value, 'NO_MaxLength' => $this->GetOption(self::NO_MaxLength)));
	    }
	
	    if ($this->HasOption(self::NO_MinLength) && $length < $this->GetOption('min_length'))
	    {
	      	throw new CExceptionValidator($this, self::NM_MinLength, array('value' => $value, 'NO_MinLength' => $this->GetOption(self::NO_MinLength)));
	    }
	
	    return $valid;
	}
}

?>